package fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
//import android.widget.ListView;

import com.game.demo409.demo409.R;
import com.yalantis.pulltorefresh.library.PullToRefreshView;

import java.util.LinkedList;

import fragments.FragmentAbstract.FindingInFmBase;
import views.LoadMoreListView;


/**
 * Created by Administrator on 2015/2/4.
 */
public class FindingHot extends FindingInFmBase {
    private static final String ARG_POSITION = "position";

    private  static final int REFRESH_DELAY = 2000;
    private  static final int LOAD_DELAY = 2000;

    private  int position;

    private PullToRefreshView mPullToRefreshView;

    private LoadMoreListView mListView;

    private ArrayAdapter<String> mAdapter;

    private LinkedList<String> data ;

    private static Handler handler = new Handler();

    private boolean isPrepared = false;

    public  static  FindingHot newInstance(int position) {
        FindingHot fa = new FindingHot();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION,position);
        fa.setArguments(b);
        return fa;
    }

    @Override

    public  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        position = getArguments().getInt(ARG_POSITION);
        data = new LinkedList<String>();
        data.add("热门活动1");
        data.add("热门活动2");
        data.add("热门活动3");
        data.add("热门活动4");
        data.add("热门活动5");
        data.add("热门活动6");
        data.add("热门活动7");
        data.add("热门活动8");
        data.add("热门活动9");
        Log.d(getActivity().toString(),"onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View hot = inflater.inflate(R.layout.findinghot_fm,container,false);

        mListView = (LoadMoreListView)hot.findViewById(R.id.hot_list);
        // all_list.setAdapter(new FindingallAdapter(list,getActivity()));
        mAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_expandable_list_item_1,data);

        mListView.setOnGetMoreListener(new LoadMoreListView.OnGetMoreListener() {
            @Override
            public void onGetMore() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.add("热门活动+");
                        mAdapter.notifyDataSetChanged();
                        mListView.getMoreComplete();
                    }
                },LOAD_DELAY);
            }
        });

        mListView.setAdapter(mAdapter);

        mPullToRefreshView = (PullToRefreshView)hot.findViewById(R.id.pull_to_refresh_hot);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.addFirst("新热门活动");
                        mAdapter.notifyDataSetChanged();
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, REFRESH_DELAY);
            }
        });


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        mListView.performGetMore();

        isPrepared = true;

        FragmentLoading();

        return hot;
    }
    @Override
    protected  void FragmentLoading() {
        if(!isPrepared||!isVisible) {
            return;
        }
        //Fragment可见就刷新
        mPullToRefreshView.setRefreshing(true);
        new Thread (new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        data.addFirst("新热门活动");
                        mAdapter.notifyDataSetChanged();
                        mPullToRefreshView.setRefreshing(false);
                    }
                },REFRESH_DELAY);
            }
        }).start();
    }

    @Override
    public void  onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
    }
    @Override
    public void onStart() {
        super.onStart();
        Log.d(getActivity().toString(),"onStart");
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.d(getActivity().toString(),"onResume");
    }
    @Override
    public  void onPause() {
        super.onPause();
        Log.d(getActivity().toString(),"onPause");

    }
    @Override
    public  void onStop() {
        super.onStop();
        Log.d(getActivity().toString(),"onStop");
    }

}
